var btnName = "";
var selectJson;
var typeStr = "";
var JobCode = "";
var cargoSpace = '';
var sendFun;
var actionName = '';
var funName = "Phone_SetStore_In_ShangJia";

(function(e) {

	mui.plusReady(function() {
		mui.init({
			beforeback: function () {
				// 刷新上一页
				plus.webview.currentWebview().opener().evalJS('refresh();');
			}
		});
		btnName = getQueryString("btnName");
		selectJson = JSON.parse(getQueryString("selectStr"));
		typeStr = getQueryString("typeStr");
		JobCode = getQueryString("JobCode");
		$("#title").html(btnName);
		
		if(btnName != '下架') {
			$('.revolve-tray').hide();
		}
		
		if(btnName == "下发") {
			$(".in-sid").hide();
			sendFun = xiafa;
		} else if(btnName == "上架") {
			sendFun = shangxiajia;
		} else if(btnName == "下架") {
			funName = "Phone_SetStore_Out_XiaJia";
			var rfid = getQueryString("rfid");
//			$('#scanInput').prop('readonly', true).val(rfid);
			sendFun = shangxiajia;
		} else if(btnName == "验证") {
			funName = "Phone_SetStore_Out_YanZheng";
			$('#cargoSpace').hide();
		}
		$("#commit").html("确认" + btnName);

	});

})(mui);

var jiaCode = "";

var huoweiCode = "";

$("#commit").click(function() {
		
	jiaCode = $("#scanInput").val().trim();
	cargoSpace = $('#cargoSpace').val().trim();
	if(isEmpty(jiaCode) || isEmpty(jiaCode.trim())) {
		mui.toast("请输入或扫描条码");
		return;
	}
	
	if(btnName == '下架' && !cargoSpace) {
		mui.toast("请输入货位");
		return;
	}
	
	if(btnName == '下架') {
		shangxiajia();
	} else if(btnName == '验证') {
		shangxiajia();
	} else {
		GetRFID(jiaCode);
	}
});

//$("#scanInput").bind("change", function() {
//	var code = this.value;
//	if(isEmpty(code) || isEmpty(code.trim())) {
//		return;
//	}
//	GetRFID(code , this)
//});

function xiafa() {

	var jsonData = {
		"op": "Phone_SetStore_In_XiaFa",
		"RFID": jiaCode,
		"Main": selectJson

	}
	if(checkParams(jsonData)) {
		mui.toast("请输入或扫描条码");
		return;
	}
	var opts = {
		url: "",
		addressUrl: "testline",
		requestType: "POST",
		pastDate: jsonData
	};

	console.log(JSON.stringify(opts));

	plus.nativeUI.showWaiting();
	crossDomainAjax(opts, function(data) {
		console.log(JSON.stringify(data));
		plus.nativeUI.closeWaiting();
		var pw = plus.webview.currentWebview().opener();
		pw.evalJS("refresh()");
		mui.back();
	}, function(s) {

	});
}

function GetRFID(code) {

	var jsonData = {
		op: "Phone_GetRFID",
		Code: code
	}
	if(checkParams(jsonData)) {
		return;
	}
	var opts = {
		url: "",
		addressUrl: "testline",
		requestType: "POST",
		pastDate: jsonData
	};

	console.log(JSON.stringify(opts));

	plus.nativeUI.showWaiting();
	crossDomainAjax(opts, function(data) {
		console.log(JSON.stringify(data));
		plus.nativeUI.closeWaiting();
		sendFun();
	}, function(s) {
		
	});
}

function shangxiajia() {
	var code = $('#scanInput').val().trim();
	if(!code) {
		mui.toast("请输入或扫描条码标签");
		return;
	}

	huoweiCode = $("#cargoSpace").val().trim();
	if(btnName != '验证' && isEmpty(huoweiCode)) {
		mui.toast("请输入或扫描货位码");
		return;
	}
	
	var ZhouZhuanCode = $('#revolve-tray').val().trim();

	var jsonData = {
		op: funName,
		JobCode: JobCode,
		TuoCode: jiaCode,
		ZhouZhuanCode: ZhouZhuanCode,
		RFID: jiaCode,
		HuoWei: huoweiCode,
		SPPlace: huoweiCode,
		Pid: userInfo.Pid
	};
	if(checkParams(jsonData)) {
		return;
	}
	var opts = {
		url: "",
		addressUrl: "testline",
		requestType: "POST",
		pastDate: jsonData
	};

	console.log(JSON.stringify(opts));

	plus.nativeUI.showWaiting();
	crossDomainAjax(opts, function(data) {
		console.log(JSON.stringify(data));
		plus.nativeUI.closeWaiting();
		var pw = plus.webview.currentWebview().opener();
		pw.evalJS("refresh()");
		mui.back();
	}, function(s) {
	});
}



